﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace XControls
{
    [ToolboxBitmap(typeof(OpaqueLayerControl))]
    public partial class OpaqueLayerControl : System.Windows.Forms.Control
    {
        #region members

        private bool m_bTransparentBG = true;
        private int m_iAlpha = 105;
        ProgressBarEx m_progress = null;

        #endregion

        #region properties

        /// <summary>
        /// 获取或设置显示文字
        /// </summary>
        [Category("OpaqueLayerControl"), Description("要显示的文字")]
        public override string Text
        {
            get
            {
                return m_progress != null ? m_progress.Text : "";
            }
            set
            {
                if (m_progress != null && m_progress.Text != value) m_progress.Text = value;
            }
        }

        /// <summary>
        /// 获取或设置显示文字
        /// </summary>
        [Category("OpaqueLayerControl"), Description("要最大长度")]
        public int Maximum
        {
            get
            {
                return m_progress != null ? m_progress.Maximum : 100;
            }
            set
            {
                if (m_progress != null && m_progress.Maximum != value) m_progress.Maximum = value;
            }
        }

        /// <summary>
        /// 获取或设置显示文字
        /// </summary>
        [Category("OpaqueLayerControl"), Description("要最大长度")]
        public int Value
        {
            get
            {
                return m_progress != null ? m_progress.Value : 1;
            }
            set
            {
                if (m_progress != null && m_progress.Value != value) m_progress.Value = value;
            }
        }

        /// <summary>
        /// 获取或设置是否显示半透明背景
        /// </summary>
        [Category("OpaqueLayerControl"), Description("是否使用透明,默认为True")]
        public bool TransparentBG
        {
            get { return m_bTransparentBG; }
            set
            {
                m_bTransparentBG = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 获取或设置背景透明度
        /// </summary>
        [Category("OpaqueLayerControl"), Description("设置透明度")]
        public int Alpha
        {
            get { return m_iAlpha; }
            set
            {
                m_iAlpha = value;
                this.Invalidate();
            }
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明
                return cp;
            }
        }
        /// <summary>
        /// 获取或设置loading宽度
        /// </summary>
        [Category("OpaqueLayerControl"), Description("设置loading宽度")]
        public int LoadingWidth
        {
            get { return m_progress != null ? m_progress.Width : 0; }
            set
            {
                if (m_progress != null && m_progress.Width != value)
                {
                    m_progress.Width = value;
                    m_progress.Location = GetCenterLocation(m_progress);
                }
            }
        }
        /// <summary>
        /// 获取或设置loading高度
        /// </summary>
        [Category("OpaqueLayerControl"), Description("设置loading高度")]
        public int LoadingHeight
        {
            get { return m_progress != null ? m_progress.Height : 0; }
            set
            {
                if (m_progress != null && m_progress.Height != value)
                {
                    m_progress.Height = value;
                    m_progress.Location = GetCenterLocation(m_progress);
                }
            }
        }

        #endregion

        #region constructors

        public OpaqueLayerControl(Control cParent)
            : this(105, true)
        {
            InitializeComponent();
            cParent.Controls.Add(this);
            this.BringToFront();
        }
        public OpaqueLayerControl(int iAlpha, bool bLoading)
        {
            BackColor = Color.FromArgb(66, 66, 66);
            Dock = DockStyle.Fill;
            SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
            this.m_iAlpha = iAlpha;
            if (bLoading)
            {
                //PictureBox pictureBox_Loading = new PictureBox();
                //pictureBox_Loading.BackColor = System.Drawing.Color.Transparent;
                ////Assembly myAssembly = Assembly.GetExecutingAssembly();
                ////Stream myStream = myAssembly.GetManifestResourceStream("XControls.Resources.Loading.gif");
                ////Bitmap bmp = new Bitmap(myStream);
                //pictureBox_Loading.Image = XControls.Properties.Resources.loading;
                //pictureBox_Loading.Name = "pictureBox_Loading";
                //pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
                //pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
                //Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中
                //pictureBox_Loading.Location = Location;
                //pictureBox_Loading.Anchor = AnchorStyles.None;
                //this.Controls.Add(pictureBox_Loading);
                m_progress = new ProgressBarEx()
                {
                    Anchor = AnchorStyles.None,
                    BackgroundPainter = new PlainBackgroundPainter(),
                    BorderPainter = new PlainBorderPainter(),
                    //ProgressPainter = new StripedProgressPainter()
                    //{
                    //    //GlossPainter = new RoundGlossPainter()
                    //    //{
                    //    //    AlphaHigh = 150,
                    //    //    AlphaLow = 0,
                    //    //    Color = Color.FromName("Maroon"),
                    //    //    Style = GlossStyle.Bottom,
                    //    //    Successor = new GradientGlossPainter() { AlphaHigh = 240, AlphaLow = 0, Angle = 90, Color = Color.White, PercentageCovered = 50, Style = GlossStyle.Top }
                    //    //},
                    //    //BaseColor = Color.FromArgb(255, 255, 128),
                    //    //StripeColor = Color.FromArgb(192, 0, 0)
                    //},
                    ProgressType = XControls.ProgressType.Smooth,
                    Maximum = this.Maximum,
                    //Value = this.Maximum != 100 ? this.Value : 100,
                    Value = this.Value,
                    Minimum = 0,
                    //MarqueeStep = 1,
                    Width = 150,
                    Height = 15,
                    Text = "Loading..."
                };
                m_progress.Location = GetCenterLocation(m_progress);
                Controls.Add(m_progress);
                //if (Visible) m_progress.StartAnimation();
                VisibleChanged += new EventHandler(OpaqueLayerControl_VisibleChanged);
            }
        }

        #endregion

        #region methods

        private Point GetCenterLocation(Control ctrl)
        {
            if (ctrl != null)
            {
                return new Point(this.Location.X + (this.Width - ctrl.Width) / 2, this.Location.Y + (this.Height - ctrl.Height) / 2);
            }
            return new Point();
        }

        #endregion

        #region events

        private void OpaqueLayerControl_VisibleChanged(object sender, EventArgs e)
        {
            if (m_progress != null)
            {
                if (Visible)
                {
                    m_progress.StartAnimation();
                }
                else
                {
                    m_progress.StopAnimation();
                }
                this.BringToFront();
            }
        }
        /// <summary>
        /// 自定义绘制窗体
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            float vlblControlWidth;
            float vlblControlHeight;
            Pen labelBorderPen;
            SolidBrush labelBackColorBrush;
            if (m_bTransparentBG)
            {
                Color drawColor = Color.FromArgb(this.m_iAlpha, this.BackColor);
                labelBorderPen = new Pen(drawColor, 0);
                labelBackColorBrush = new SolidBrush(drawColor);
            }
            else
            {
                labelBorderPen = new Pen(this.BackColor, 0);
                labelBackColorBrush = new SolidBrush(this.BackColor);
            }
            vlblControlWidth = this.Size.Width;
            vlblControlHeight = this.Size.Height;
            e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
            e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);

        }

        #endregion
    }
}